﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;
using OpenNETCF.Drawing.Imaging;
using HandRise.MBHOP.Controls;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmPictureSee : FrmBase
    {
        private LinkFile linkFile;

        private Plan plan = null;

        private LinkFileService linkFileService = new LinkFileService();

        public FrmPictureSee()
        {
            InitializeComponent();
            ttlbrhead.Text = ResourceImage.地面巡视;
            ilstbcup.Images.Add(ResourceImage.修改01);
            ilstbcup.Images.Add(ResourceImage.删除01);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.修改02);
            ilstdown.Images.Add(ResourceImage.删除02);
            ilstdown.Images.Add(ResourceImage.返回02);
        }

        private void FrmPictureSee_Load(object sender, EventArgs e)
        {
            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            tbPictureTitle.Text = linkFile.Title;

            //显示图片
            try
            {
                ImagingFactory factory = new ImagingFactoryClass();
                IImage img;
                factory.CreateImageFromFile(linkFile.FullFileName, out img);
                IBitmapImage imgB;
                factory.CreateBitmapFromImage(img,
                                              (uint)pbImage.Width,
                                              (uint)pbImage.Height,
                                              System.Drawing.Imaging.PixelFormat.Format24bppRgb,
                                              InterpolationHint.InterpolationHintDefault,
                                              out imgB);
                pbImage.Image = ImageUtils.IBitmapImageToBitmap(imgB);
            }
            catch (Exception ex)
            {
                //MessageBox.Show("图片装载错误!");
            }

        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            linkFile = (LinkFile)Parameter[0];

            if(Parameter.Count > 1)
                plan = (Plan)Parameter[1];
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            linkFile = (LinkFile)Caches[0];

            if (Caches.Count > 1)
                plan = (Plan)Caches[1];
        }

        private void bcOperate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {

                case 0: //修改
                    EditPhoto();
                    break;
                case 1: //删除
                    DeletePhoto();
                    break;
                case 2: //返回
                    FormManage.GotoNext("FrmPictureList",null, null, true);
                    break;
            }
        }

        private void EditPhoto()
        {
            if (linkFile.ReportState == "已上报")
            {
                mssdlgComfirm.ShowDialog(this, "当前照片已经上报，不能修改", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            if (plan != null && plan.State == "已完成")
            {
                mssdlgComfirm.ShowDialog(this, "当前计划已完成，照片不能修改", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            FormManage.GotoNext("FrmPctupData", new object[] { linkFile }, new object[] { linkFile,plan }, false);
        }

        private void DeletePhoto()
        {
            if (linkFile.ReportState == "已上报")
            {
                mssdlgComfirm.ShowDialog(this, "当前照片已经上报，不能删除", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            if (plan != null && plan.State == "已完成")
            {
                mssdlgComfirm.ShowDialog(this, "当前计划已完成，照片不能删除", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            if (mssdlgexec.ShowDialog(this, "确认删除该照片？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button1)
            {
                linkFileService.DeleteLinkFile(linkFile.LinkFileID);
                FormManage.GotoNext("FrmPictureList", null, null, true);
            }
        }

    }
}